package com.gin.no_gay_alliance.fragment.user.tab;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.gin.no_gay_alliance.databinding.DialogItemUseBinding;
import com.gin.no_gay_alliance.databinding.FragmentUserCenterTabUserItemInventory2Binding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.fragment.item.InventoryFragment;
import com.gin.no_gay_alliance.fragment.item.StoreFragment;
import com.gin.no_gay_alliance.param.ItemParam;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

/**
 * 用户道具
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/7 15:15
 **/
public class UserItemTabFragment extends BaseBindingFragment<FragmentUserCenterTabUserItemInventory2Binding> implements TabLayoutMediator.TabConfigurationStrategy {
    private static final String ARG_KEY = ItemParam.class.getName();
    /**
     * 道具参数
     */
    private ItemParam param;
    private TabLayoutMediator tabLayoutMediator;

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        b.vp2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                if (position == 0) {
                    return InventoryFragment.newInstance(param);
                } else {
                    return StoreFragment.newInstance(param);
                }
            }

            @Override
            public int getItemCount() {
                return 2;
            }
        });
        // 处于使用状态下，优先选择商店标签
        if (param.isToUser() || param.isToReply()) {
            b.vp2.setCurrentItem(1);
        }
        tabLayoutMediator = attachTabLayout(tabLayoutMediator, b.tabLayout, b.vp2);
    }


    /**
     * 获取和处理参数
     *
     * @param arguments 参数
     */
    @Override
    protected void handleArguments(Bundle arguments) {
        if (arguments != null && arguments.getParcelable(ARG_KEY) != null) {
            param = arguments.getParcelable(ARG_KEY);
        } else {
            throw new RequireParamException(ARG_KEY);
        }
    }

    /**
     * 绑定tabLayout 和 ViewPager2
     */
    @Override
    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
        if (position == 0) {
            tab.setText("库存");
        } else {
            tab.setText("商店");
        }
    }

    public static UserItemTabFragment newInstance() {
        return newInstance(new ItemParam());
    }

    public static UserItemTabFragment newInstance(ItemParam itemParam) {
        UserItemTabFragment fragment = new UserItemTabFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(ARG_KEY, itemParam);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 打开一个道具弹窗
     *
     * @param activity  活动
     * @param itemParam 道具参数
     */
    public static void showDialog(FragmentActivity activity, ItemParam itemParam) {
        showDialog(new FragmentStateAdapter(activity) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return UserItemTabFragment.newInstance(itemParam);
            }

            @Override
            public int getItemCount() {
                return 1;
            }
        }, activity);
    }

    /**
     * 打开一个道具弹窗
     *
     * @param fragment  活动
     * @param itemParam 道具参数
     */
    public static void showDialog(Fragment fragment, ItemParam itemParam) {
        showDialog(new FragmentStateAdapter(fragment) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return UserItemTabFragment.newInstance(itemParam);
            }

            @Override
            public int getItemCount() {
                return 1;
            }
        }, fragment.requireContext());
    }

    /**
     * 打开一个道具弹窗
     *
     * @param adapter 适配器
     * @param context 上下文
     */
    private static void showDialog(FragmentStateAdapter adapter, Context context) {
        DialogItemUseBinding b = DialogItemUseBinding.inflate(LayoutInflater.from(context));
        b.vp2.setAdapter(adapter);

        AlertDialog dialog = AlertDialogUtils.showConfirm(context, "选择道具", null, b.getRoot(), null);

        // 手动指定大小
        if (dialog.getWindow() != null) {
            int width = LinearLayout.LayoutParams.MATCH_PARENT;
            int height = LinearLayout.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().setLayout(width, height);
        }
    }
}
